﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cocon90.Lib.Util.Utility
{
    /// <summary>
    /// 克隆辅助类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class cloneHelper<T>
    {
        /// <summary>
        /// 克隆模型 取得新实例。
        /// </summary>
        /// <param name="sourceModel"></param>
        /// <returns></returns>
        public static T CloneModel(T sourceModel)
        {
            var type = typeof(T);
            var properties = type.GetProperties();
            var newModel = (T)Activator.CreateInstance(type);
            foreach (var prop in properties)
            {
                var oldPropValue = prop.GetValue(sourceModel, null);
                prop.SetValue(newModel, oldPropValue, null);
            }
            return newModel;
        }
    }
}
